package com.daihou.BCJ.resource;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.daihou.config.TheAuth;
import com.daihou.model.ceres.ResourceUrlEnum;
import com.daihou.utils.DHConfigFile;
import com.daihou.utils.DHHttpUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.testng.Assert;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
 * @author liujintao
 * @date 2021/2/3 15:39
 */
public class Numpool {
    private static final Logger logger = LoggerFactory.getLogger(Numpool.class);
    private String url;
    Map<String, String> headers = new HashMap<>();
    private int id;

    @BeforeTest(description = "测试准备工作,获取Authorization对象")
    public void beforeTest() throws IOException {
        url = DHConfigFile.getUrl("DaiHou_URL","resource", ResourceUrlEnum.INSERT.toString());
        //设置请求头中的Authorization
        headers.put("Authorization", TheAuth.getAuth());
        headers.put("X-UserName", "admin");
    }

    @Test(description = "号码池插入数据")
    public void numpollInsertTest() {
        JSONObject body = new JSONObject();
        body.put("outCallCompany", "XA");
        body.put("sitNo", "235415");
        body.put("organ", "02");
        String result = Objects.requireNonNull(DHHttpUtil.post_with_String(url, body.toJSONString(), headers)).toJSONString();
        logger.info("属性查询结果:{}", result);
        Assert.assertTrue(result.contains("\"message\":\"操作成功\",\"status\":\"0\""));
    }

    @Test(description = "号码池查询",dependsOnMethods ="numpollInsertTest" )
    public void numpoolQueryTest() {
        url = DHConfigFile.getUrl("DaiHou_URL","resource", ResourceUrlEnum.NUMBERPOOLQUERY.toString());
        JSONObject body = new JSONObject();
        body.put("pageNum", 1);
        body.put("pageSize", 20);
        body.put("sitNo", "235415");
        String result = Objects.requireNonNull(DHHttpUtil.post_with_String(url, body.toJSONString(), headers)).toJSONString();
        logger.info("号码池查询结果:{}", result);
        Assert.assertTrue(result.contains("\"message\":\"操作成功\",\"status\":\"0\""));
        id = JSON.parseObject(result).getJSONObject("data").getJSONArray("list").getJSONObject(0).getIntValue("id");
    }

    @Test(description = "号码池更新数据",dependsOnMethods ="numpoolQueryTest" )
    public void numpollUpdateTest() {
        url = DHConfigFile.getUrl("DaiHou_URL","resource", ResourceUrlEnum.UPDATE.toString());

        JSONObject body = new JSONObject();
        body.put("id", id);
        body.put("outCallCompany", "XA");
        body.put("organ", "BJYYZX");
        body.put("pageIndex", 1);
        body.put("sitNo", "235415");
        String result = Objects.requireNonNull(DHHttpUtil.post_with_String(url, body.toJSONString(), headers)).toJSONString();
        logger.info("属性查询结果:{}", result);
        Assert.assertTrue(result.contains("\"message\":\"操作成功\",\"status\":\"0\""));
    }

    @Test(description = "号码池删除数据",dependsOnMethods ="numpollUpdateTest" )
    public void numpollDeleteTest() {
        url = DHConfigFile.getUrl("DaiHou_URL","resource", ResourceUrlEnum.DEL.toString());
        JSONObject body = new JSONObject();
        body.put("id", String.valueOf(id));
        String result = Objects.requireNonNull(DHHttpUtil.post_with_String(url, body.toJSONString(), headers)).toJSONString();
        logger.info("属性查询结果:{}", result);
        Assert.assertTrue(result.contains("\"message\":\"操作成功\",\"status\":\"0\""));
    }
}